<template>
  <div class="wrap">
    <div class="form-content">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="ID">
          <el-input v-model="formInline.jsid" placeholder="ID查询..." style="width: 100px"></el-input>
        </el-form-item>
        <el-form-item label="教室楼栋">
          <el-input v-model="formInline.jsld" placeholder="楼栋查询..." style="width: 100px"></el-input>
        </el-form-item>
        <el-form-item label="教室房号">
          <el-input v-model="formInline.jsfh" placeholder="房号查询..." style="width: 100px"></el-input>
        </el-form-item>
        <el-form-item label="教室座位数">
          <el-input v-model="formInline.jszws" placeholder="座位数查询..." style="width: 100px"></el-input>
        </el-form-item>
        <el-form-item label="损坏启用">
          <el-select v-model="formInline.sfqy" placeholder="损坏启用查询..." style="width: 100px">
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否开启给学生">
          <el-select v-model="formInline.sfkfgxs" placeholder="是否开启查询" style="width: 100px">
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="primary" v-on:click="dialogForm2Visible=true">添加</el-button>
        </el-form-item>
      </el-form>
    </div>


<!--    查询显示内容-->
    <el-table
      :data="classRoomList"
      style="width: 100%"
      >
      <el-table-column
        width="100"
        prop="jsid"
        label="ID"
        >
      </el-table-column>
      <el-table-column
        width="150"
        prop="jsld"
        label="楼栋"
        >
      </el-table-column>
      <el-table-column
        width="150"
        prop="jsfh"
        label="房号">
      </el-table-column>
      <el-table-column
        width="150"
        prop="jszws"
        label="座位总数">
      </el-table-column>
      <el-table-column
        width="150"
        prop="sfqy"
        label="是否启用">
      </el-table-column>
      <el-table-column
        width="150"
      prop="sfkfgxs"
      label="开放给学生">
    </el-table-column>
      <el-table-column
        width="150"
        prop="shzws"
        label="损坏座位数">
      </el-table-column>
      <el-table-column
        width="150"
        prop="shxx"
        label="损坏信息">
      </el-table-column>
      <el-table-column
        align="center"  width="250" label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">修 改</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删 除</el-button>
        </template>
      </el-table-column>
    </el-table>


<!--    修改信息内容-->
    <el-dialog title="修改信息" :visible.sync="dialogFormVisible">
      <el-form>
        <el-form-item label="楼栋号" :label-width="formLabelWidth">
          <el-input  autocomplete="off" v-model="editClassRoom.jsid"></el-input>
        </el-form-item>
        <el-form-item label="楼栋名称" :label-width="formLabelWidth">
          <el-input  autocomplete="off" v-model="editClassRoom.jsld"></el-input>
        </el-form-item>
        <el-form-item label="房间号" :label-width="formLabelWidth">
          <el-input  autocomplete="off" v-model="editClassRoom.jsfh"></el-input>
        </el-form-item>
        <el-form-item label="教室座位数" :label-width="formLabelWidth">
          <el-input  autocomplete="off" v-model="editClassRoom.jszws"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" :label-width="formLabelWidth">
          <el-select v-model="editClassRoom.sfqy" style="width: 100px">
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否开启给学生" :label-width="formLabelWidth">
          <el-select v-model="editClassRoom.sfkfgxs"style="width: 100px">
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="损坏座位数" :label-width="formLabelWidth">
          <el-input  autocomplete="off" v-model="editClassRoom.shzws"></el-input>
        </el-form-item>
        <el-form-item label="损坏信息" :label-width="formLabelWidth">
          <el-input  autocomplete="off" v-model="editClassRoom.shxx"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false;editClassRoomf()">确 定</el-button>
      </div>
    </el-dialog>

<!--    添加信息内容-->
    <el-dialog title="添加信息" :visible.sync="dialogForm2Visible">
      <el-form >
        <el-form-item label="楼栋号" :label-width="formLabelWidth">
          <el-input  autocomplete="off" v-model="classRoomAdd.jsid"></el-input>
        </el-form-item>
        <el-form-item label="楼栋名称" :label-width="formLabelWidth">
          <el-input  autocomplete="off" v-model="classRoomAdd.jsld"></el-input>
        </el-form-item>
        <el-form-item label="房间号" :label-width="formLabelWidth">
          <el-input  autocomplete="off" v-model="classRoomAdd.jsfh"></el-input>
        </el-form-item>
        <el-form-item label="总座位数" :label-width="formLabelWidth">
          <el-input  autocomplete="off" v-model="classRoomAdd.jszws"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" :label-width="formLabelWidth">
          <el-select v-model="classRoomAdd.sfqy" style="width: 100px">
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否开启给学生" :label-width="formLabelWidth">
          <el-select v-model="classRoomAdd.sfkfgxs"style="width: 100px">
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="损坏座位数" :label-width="formLabelWidth">
          <el-input  autocomplete="off" v-model="classRoomAdd.shzws"></el-input>
        </el-form-item>
        <el-form-item label="损坏信息" :label-width="formLabelWidth">
          <el-input  autocomplete="off" v-model="classRoomAdd.shxx"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogForm2Visible = false">取 消</el-button>
        <el-button type="primary" @click="dialogForm2Visible = false;adduser()">确 定</el-button>
      </div>
    </el-dialog>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

  </div>
</template>

<script>
import { getAllClassRoomInfo ,addClassRoom ,editClassRoomById,deleteClassRoomById,getClassRoomInfo} from '@/api/admin/classroom'
import qs from 'qs'


export default {
  name: "classroom",
  data(){
    return{
      classRoomList:[],
      dialogFormVisible: false,
      dialogForm2Visible:false,
      formLabelWidth: '120px',
      classRoomAdd:{},
      editClassRoom:{},
      formInline: {
        // jsid: '', jsld: '', jsfh: '', jszws: '', sfqy: '', sfkfgxs: '',
      },
      pageformInline:{},
      currentPage: 1,
      total:1,
      pageSize:5,pageNum: 1
    }
  },

  created() {
    this.queryAll();
  },
  methods:{
    handleSizeChange(val) {
      this.pageSize=val;
      getClassRoomInfo(qs.stringify(this.pageformInline),this.pageNum,this.pageSize).then(res => {
        if (res.status==200){
          this.classRoomList =  res.data.classRooms
          this.total = res.data.total
        }
      }).catch(err =>{
        console.log('查询class失败',err)
      })
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      getClassRoomInfo(qs.stringify(this.pageformInline),this.pageNum,this.pageSize).then(res => {
        if (res.status==200){
          this.classRoomList =  res.data.classRooms
          this.total = res.data.total
        }
      }).catch(err =>{
        console.log('查询class失败',err)
      })
    },
    onSubmit() {
      // console.log(this.formInline);
      getClassRoomInfo(qs.stringify(this.formInline),this.pageNum,this.pageSize).then(res => {
        if (res.status==200){
          this.classRoomList =  res.data.classRooms
          this.total = res.data.total
          this.pageformInline=this.formInline
        }
      }).catch(err =>{
        console.log('查询class失败',err)
      }).finally(() =>{
        this.formInline={}
      })
    },
    adduser(){
      let userData = {
        jsid:this.classRoomAdd.jsid,
        jsld:this.classRoomAdd.jsld,
        jsfh:this.classRoomAdd.jsfh,
        jszws:this.classRoomAdd.jszws,
        sfqy:this.classRoomAdd.sfqy,
        shzws:this.classRoomAdd.shzws,
        shxx:this.classRoomAdd.shxx,
        sfkfgxs:this.classRoomAdd.sfkfgxs,
      }
      let data = qs.stringify(userData);

      addClassRoom(data).then(res =>{
        if (res.status==200){
          this.classRoomList.push(this.classRoomAdd)
          this.classRoomAdd={jsid:'', jsld:'', jsfh:'', jszws:'', sfqy:'', shzws:'', shxx:'', sfkfgxs:'',}
        }else {
          console.log("添加失败")
        }
      }).catch(err => {
        console.log('添加class失败',err)
      })
    },
    editClassRoomf(){
      let userData = {
        jsid:this.editClassRoom.jsid,
        jsld:this.editClassRoom.jsld,
        jsfh:this.editClassRoom.jsfh,
        jszws:this.editClassRoom.jszws,
        sfqy:this.editClassRoom.sfqy,
        shzws:this.editClassRoom.shzws,
        shxx:this.editClassRoom.shxx,
        sfkfgxs:this.editClassRoom.sfkfgxs
      }
      let data = qs.stringify(userData);

      editClassRoomById(data).then( res =>{
        if (res.status==200){
          //
        }
      }).catch(err => {
        console.log('更新class失败',err)
      })
    },
    queryAll() {
      getAllClassRoomInfo(this.pageNum,this.pageSize).then( res =>{
        this.classRoomList = res.data.classRooms
        this.total = res.data.total
      }).catch(err => {
        console.log('获取classRooms失败',err)
      })
    },
    handleEdit(index, row) {
      this.editClassRoom=this.classRoomList[index]
      this.dialogFormVisible = true
    },
    handleDelete(index, row) {

      deleteClassRoomById(qs.stringify({classRoomId : this.classRoomList[index].jsid})).then(res =>{
        this.classRoomList.splice(index,1)
      }).catch(function (error) {
        //console.log(error);
      });

    }
  }
}
</script>

<style lang="scss" scoped>
  .wrap{
    width: 1400px;
    height: 100%;
    margin-left: 10px;
    margin-top: 10px;
    /*background-color: #3a835d;*/
  }
  .el-table{
    width: 2000px;
  }

  .form-content{
    width: 100%;
    border-bottom: 2px solid #CCCCCC;
  }
  .el-table th.is-right, .el-table td.is-right {
    text-align: center;
  }


</style>
